/*
 * Copyright 2011 Clean GWT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleanform.gwt.chinesechess.client;

import com.cleanform.gwt.page.client.IndexedApplicationModule;
import com.cleanform.gwt.page.client.PageContext;
import com.cleanform.gwt.web.client.ObjectCallback;
import com.cleanform.gwt.web.client.Requests;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

/**
 * The entry point of the application.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class Chinesechess extends IndexedApplicationModule {

	private static String user;
	
	public static String getUser(){
		return user;
	}
	
	public static void setUser(String user){
		Chinesechess.user = user;
	}
	
	@Override
	protected Widget getIndexPage() {
		return GWT.create(Login.class);
	}

	@Override
	protected void initialize(final PageContext pageContext) {

		GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler(){

			@Override
			public void onUncaughtException(Throwable e) {
				e.printStackTrace();
				Window.alert(e.getMessage());
			}});

		Requests.post("gameController/getUser.do", null, new ObjectCallback<String>(String.class) {

			@Override
			protected void onCallback(String result) {
				user = result;
				if (result == null || result.isEmpty()) {
					History.newItem(Login.class.getSimpleName());
				} else {
					pageContext.setDefaultPage(Login.class.getSimpleName());
					History.fireCurrentHistoryState();
				}
			}
		});
	}

}
